package com.ciwong.epaper.modules.bookstore.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.StudentGroupBean;
import com.ciwong.epaper.modules.me.ui.GroupingStudentActivity;
import com.ciwong.epaper.widget.NoScrollListView;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 学生分组组名适配器
 * @author: wyk
 * @time: 2021/5/24 16:21
 */
public class GroupingStuAdapter extends BaseExpandableListAdapter {

    private List<StudentGroupBean> studentGroupBeanList = new ArrayList<>();

    private GroupingStudentActivity mContext;

    private int classId;

    private GroupingStuListAdapter groupingStuListAdapter;

    public GroupingStuAdapter(List<StudentGroupBean> studentGroupBeanList, GroupingStudentActivity mContext, int classId) {
        this.mContext = mContext;
        this.studentGroupBeanList = studentGroupBeanList;
        this.classId = classId;
    }

    @Override
    public int getGroupCount() {
        return null == studentGroupBeanList ? 0 : studentGroupBeanList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return null == studentGroupBeanList ? 0 : 1;
    }

    @Override
    public Object getGroup(int i) {
        return null == studentGroupBeanList ? null : studentGroupBeanList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return null == studentGroupBeanList ? null : studentGroupBeanList.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewHolderGroup viewHolderGroup;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_grouping_stu_parent, viewGroup, false);
            viewHolderGroup = new ViewHolderGroup();
            viewHolderGroup.stu_class_group_text = view.findViewById(R.id.stu_class_group_text);
            viewHolderGroup.grouping_stu_right = view.findViewById(R.id.grouping_stu_right);
            viewHolderGroup.stu_class_text = view.findViewById(R.id.stu_class_text);
            view.setTag(viewHolderGroup);
        } else {
            viewHolderGroup = (ViewHolderGroup) view.getTag();
        }
        if (null != studentGroupBeanList) {
            String teamName = studentGroupBeanList.get(i).getList().get(0).getTeamName();
            int stuCount = studentGroupBeanList.get(i).getList().size();
            if (null != teamName && !"".equals(teamName)) {
                viewHolderGroup.stu_class_group_text.setText(teamName + "(" + stuCount + ")");
                viewHolderGroup.stu_class_text.setText("编辑");
            } else {
                viewHolderGroup.stu_class_group_text.setText("未分组" + "(" + stuCount + ")");
                viewHolderGroup.stu_class_text.setText("分组");
            }
        }

        return view;
    }

    @Override
    public View getChildView(final int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewHoldChild viewHoldChild;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_grouping_stu_child, viewGroup, false);
            viewHoldChild = new ViewHoldChild();
            viewHoldChild.grouping_stu_list = view.findViewById(R.id.grouping_stu_list);
            view.setTag(viewHoldChild);
        } else {
            viewHoldChild = (ViewHoldChild) view.getTag();
        }
        if (null != studentGroupBeanList) {
            groupingStuListAdapter = new GroupingStuListAdapter(mContext, studentGroupBeanList.get(i).getList(), classId);
            viewHoldChild.grouping_stu_list.setAdapter(groupingStuListAdapter);
        }

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private static class ViewHolderGroup {
        private TextView stu_class_group_text;
        private ImageView grouping_stu_right;
        private TextView stu_class_text;
    }

    private static class ViewHoldChild {
        private NoScrollListView grouping_stu_list;
    }

    public void setNewData(List<StudentGroupBean> studentGroupBeans) {
        studentGroupBeanList = studentGroupBeans;
        this.notifyDataSetChanged();
    }
}
